package marksys.free.com.marksys.ui.fragment.clean;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import marksys.free.com.marksys.R;
import marksys.free.com.marksys.databinding.ListFloorViewBinding;
import marksys.free.com.marksys.databinding.RecordBeforeItemBinding;
import marksys.free.com.marksys.databinding.RecordClassBeforeBinding;
import marksys.free.com.marksys.databinding.RecordClassTodayBinding;
import marksys.free.com.marksys.databinding.RecordTodayItemBinding;
import marksys.free.com.marksys.databinding.RecordWeekBinding;
import marksys.free.com.marksys.nettool.vo.ScoreRecords;
import marksys.free.com.marksys.ui.common.BaseLoadMoreAdapter;
import marksys.free.com.marksys.ui.common.DataBoundAdapter;
import marksys.free.com.marksys.ui.common.DataBoundClickListener;
import marksys.free.com.marksys.ui.fragment.ObjectUtils;

public class RecordCleanAdapter extends BaseLoadMoreAdapter<RecordCleanViewModel.CleanDayRecord, RecordWeekBinding> {
    static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
    private DataBoundClickListener<ScoreRecords> deleteClickListener;
    private DataBoundClickListener<ScoreRecords> changeClickListener;

    public RecordCleanAdapter() {
        showNoMoreView(true);
    }

    public void setItemClickListener(DataBoundClickListener<ScoreRecords> clickListener) {
        this.clickListener = clickListener;
    }

    public void setOnItemDeleteClickListener(DataBoundClickListener<ScoreRecords> deleteClickListener) {
        this.deleteClickListener = deleteClickListener;
    }

    public void setOnItemChangeClickListener(DataBoundClickListener<ScoreRecords> changeClickListener) {
        this.changeClickListener = changeClickListener;
    }

    private DataBoundClickListener<ScoreRecords> clickListener;

    @Override
    protected RecordWeekBinding createNormalBinding(ViewGroup parent, int viewType) {
        return DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.record_week, parent, false);
    }

    @Override
    protected void bindingNormal(RecordWeekBinding binding, RecordCleanViewModel.CleanDayRecord Cleandata) {
        try {
            String today = simpleDateFormat.format(System.currentTimeMillis());
            Date day = simpleDateFormat.parse(Cleandata.cleanData);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(day);
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            if (null != today && today.equals(Cleandata.cleanData)) {
                binding.setDayOfWeek("今天" + "（" + Cleandata.cleanData + "）");
            } else {
                int i = dayOfWeek - 1;
                if (i == 0) {
                    binding.setDayOfWeek("星期日" + "（" + Cleandata.cleanData + "）");
                } else {
                    String weekday = "";
                    switch (i) {
                        case 1:
                            weekday = "一";
                            break;
                        case 2:
                            weekday = "二";
                            break;
                        case 3:
                            weekday = "三";
                            break;
                        case 4:
                            weekday = "四";
                            break;
                        case 5:
                            weekday = "五";
                            break;
                        case 6:
                            weekday = "六";
                            break;
                        default:
                            break;
                    }
                    binding.setDayOfWeek("星期" + weekday + "（" + Cleandata.cleanData + "）");
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<RecordCleanViewModel.CleanClassRecord> cleanClassRecords = Cleandata.records;
        CleanClassAdapter adapter = new CleanClassAdapter(cleanClassRecords);
        adapter.setItemClickListener(this.clickListener);
        adapter.setOnItemChangeClickListener(this.changeClickListener);
        adapter.setOnItemDeleteClickListener(this.deleteClickListener);
        binding.recyclerView1.setAdapter(adapter);

    }

    @Override
    protected <VT extends ViewDataBinding> VT createFooterBinding(ViewGroup parent, int viewType) {
        return DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.list_floor_view, parent, false);
    }

    @Override
    protected void bindingFooter(ViewDataBinding binding, boolean hasMoreData, Object data) {
        if (binding instanceof ListFloorViewBinding) {
            if (hasMoreData) {
                ((ListFloorViewBinding) binding).hint.setText("正在加载更多数据...");
            } else {
                ((ListFloorViewBinding) binding).hint.setText("没有更多数据！");
            }
        }
    }

    @Override
    protected boolean areItemsTheSame(RecordCleanViewModel.CleanDayRecord oldItem, RecordCleanViewModel.CleanDayRecord newItem) {
        return oldItem.cleanData != null || oldItem.cleanData.equals(newItem.cleanData);
    }

    @Override
    protected boolean areContentsTheSame(RecordCleanViewModel.CleanDayRecord oldItem, RecordCleanViewModel.CleanDayRecord newItem) {

        if (oldItem.records == null || newItem.records == null || oldItem.records.size() != newItem.records.size()) {
            return false;
        } else {
            for (int i = 0; i < oldItem.records.size(); i++) {
                RecordCleanViewModel.CleanClassRecord oldRecords = oldItem.records.get(i);
                RecordCleanViewModel.CleanClassRecord newRecords = newItem.records.get(i);
                if (!ObjectUtils.equals(oldRecords, newRecords)) {
                    return false;
                }
            }
        }
        return true;
    }

    public static class CleanClassAdapter extends DataBoundAdapter<RecordCleanViewModel.CleanClassRecord, ViewDataBinding> {

        public static final int VIEW_TYPE_TODAY = 99077908;
        private DataBoundClickListener<ScoreRecords> clickListener;
        private DataBoundClickListener<ScoreRecords> deleteClickListener;
        private DataBoundClickListener<ScoreRecords> changeClickListener;

        public CleanClassAdapter(List<RecordCleanViewModel.CleanClassRecord> Cleanitems) {
            super(Cleanitems);
        }

        public void setItemClickListener(DataBoundClickListener<ScoreRecords> clickListener) {
            this.clickListener = clickListener;
        }

        public void setOnItemDeleteClickListener(DataBoundClickListener<ScoreRecords> deleteClickListener) {
            this.deleteClickListener = deleteClickListener;
        }

        public void setOnItemChangeClickListener(DataBoundClickListener<ScoreRecords> changeClickListener) {
            this.changeClickListener = changeClickListener;
        }


        @Override
        protected ViewDataBinding createBinding(ViewGroup parent, int viewType) {
            if (viewType == VIEW_TYPE_TODAY) {
                return DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.record_class_today, parent, false);
            } else {
                return DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.record_class_before, parent, false);
            }
        }

        @Override
        protected void bind(ViewDataBinding binding, RecordCleanViewModel.CleanClassRecord Cleanitem) {
            if (binding instanceof RecordClassTodayBinding) {
                ((RecordClassTodayBinding) binding).setClassName(Cleanitem.cleanName);
                List<ScoreRecords> scoreRecords = Cleanitem.CleanscoreRecords;
                CleanTodayRecordAdapter adapter = new CleanTodayRecordAdapter(scoreRecords);
                adapter.setItemClickListener(this.clickListener);
                adapter.setDeleteClickListener(this.deleteClickListener);
                adapter.setChangeClickListener(this.changeClickListener);
                ((RecordClassTodayBinding) binding).recyclerView2.setAdapter(adapter);

            } else if (binding instanceof RecordClassBeforeBinding) {
                ((RecordClassBeforeBinding) binding).setClassName(Cleanitem.cleanName);
                List<ScoreRecords> CleanscoreRecords = Cleanitem.CleanscoreRecords;
                CleanBeforeRecordAdapter CleanbeforeRecordAdapter = new CleanBeforeRecordAdapter(CleanscoreRecords);
                CleanbeforeRecordAdapter.setItemClickListener(this.clickListener);
                ((RecordClassBeforeBinding) binding).recyclerView1.setAdapter(CleanbeforeRecordAdapter);
            }

        }

        public String today() {
            return simpleDateFormat.format(System.currentTimeMillis());
        }

        @Override
        public int getItemViewType(int position) {
            RecordCleanViewModel.CleanClassRecord Cleanitem = getItem(position);
            if (null != Cleanitem.cleanTime && Cleanitem.cleanTime.equals(today())) {
                return VIEW_TYPE_TODAY;
            } else {
                return super.getItemViewType(position);
            }
        }
    }

    public static class CleanTodayRecordAdapter extends DataBoundAdapter<ScoreRecords, RecordTodayItemBinding> {


        private DataBoundClickListener<ScoreRecords> deleteClickListener;
        private DataBoundClickListener<ScoreRecords> changeClickListener;
        private DataBoundClickListener<ScoreRecords> clickListener;

        public CleanTodayRecordAdapter(List<ScoreRecords> items) {
            super(items);
        }

        public void setDeleteClickListener(DataBoundClickListener<ScoreRecords> deleteClickListener) {
            this.deleteClickListener = deleteClickListener;
        }

        public void setChangeClickListener(DataBoundClickListener<ScoreRecords> changeClickListener) {
            this.changeClickListener = changeClickListener;
        }

        public void setItemClickListener(DataBoundClickListener<ScoreRecords> clickListener) {
            this.clickListener = clickListener;
        }

        @Override
        protected RecordTodayItemBinding createBinding(ViewGroup parent, int viewType) {
            RecordTodayItemBinding todayItemBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.record_today_item, parent, false);
            todayItemBinding.setEventHandler(clickListener);
            todayItemBinding.setDeleteEventHandler(deleteClickListener);
            todayItemBinding.setChangeEventHandler(changeClickListener);
            return todayItemBinding;
        }

        @Override
        protected void bind(RecordTodayItemBinding binding, ScoreRecords cleanItem) {
            binding.setRecord(cleanItem);
        }
    }

    public static class CleanBeforeRecordAdapter extends DataBoundAdapter<ScoreRecords, RecordBeforeItemBinding> {
        private DataBoundClickListener<ScoreRecords> clickListener;

        public CleanBeforeRecordAdapter(List<ScoreRecords> Cleanitems) {
            super(Cleanitems);
        }

        @Override
        protected RecordBeforeItemBinding createBinding(ViewGroup parent, int viewType) {
            RecordBeforeItemBinding beforeItemBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.record_before_item, parent, false);
            beforeItemBinding.setEventHandler(clickListener);
            return beforeItemBinding;
        }

        @Override
        protected void bind(RecordBeforeItemBinding binding, ScoreRecords Cleanitem) {
            binding.setRecord(Cleanitem);
        }

        public void setItemClickListener(DataBoundClickListener<ScoreRecords> clickListener) {
            this.clickListener = clickListener;
        }
    }


}
